package com.yhq.oj.model.vo;

import cn.hutool.json.JSONUtil;
import com.google.gson.Gson;
import com.yhq.oj.judge.sandbox.model.JudgeInfo;
import com.yhq.oj.model.entity.QuestionSubmit;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 题目视图
 *
 * @author yhq
 */
@Data
public class QuestionSubmitVO implements Serializable {
    private static final Gson GSON = new Gson();

    /**
     * id
     */
    // // @TableId(value = "id", type = IdType.ASSIGN_ID)
    private Long id;

    /**
     * 使用的编程语言
     */
    // @TableField(value = "language")
    private String language;

    /**
     * 程序代码
     */
    // @TableField(value = "code")
    private String code;

    /**
     * 备注
     */
    // @TableField(value = "content")
    private String content;
    /**
     * 判题信息
     */
//    @TableField(value = "judgeInfo")
    private JudgeInfo judgeInfo;


    /**
     * 题解所用算法标签列表(json数组)
     */
    // @TableField(value = "tags")
    private List<String> tags;

    /**
     * 执行状态(0:待判题 1:判题中 2:成功 3: 解答出错 4:超出时间限制 5:超出空间限制 6:编译出错)
     */
    // @TableField(value = "status")
    private Integer status;

    /**
     * 答题用户 id
     */
    // @TableField(value = "userId")
    private Long userId;
    /**
     * 用户视图
     */
    private UserVO userVO;

    /**
     * 题目 id
     */
    // @TableField(value = "questionId")
    private Long questionId;

    /**
     * 创建时间
     */
    // @TableField(value = "createTime")
    private Date createTime;

    private static final long serialVersionUID = 1L;

    /**
     * 包装类转对象
     *
     * @param questionSubmitVO
     * @return
     */
    public static QuestionSubmit voToObj(QuestionSubmitVO questionSubmitVO) {
        if (questionSubmitVO == null) {
            return null;
        }
        QuestionSubmit questionSubmit = new QuestionSubmit();
        BeanUtils.copyProperties(questionSubmitVO, questionSubmit);
        List<String> tagList = questionSubmitVO.getTags();
        if (tagList != null) {
            questionSubmit.setTags(GSON.toJson(tagList));
        }
        JudgeInfo judgeInfo = questionSubmitVO.getJudgeInfo();
        if(judgeInfo != null){
            questionSubmit.setJudgeInfo(GSON.toJson(judgeInfo));
        }
        return questionSubmit;
    }

    /**
     * 对象转包装类
     *
     * @param questionSubmit
     * @return
     */
    public static QuestionSubmitVO objToVo(QuestionSubmit questionSubmit) {
        if (questionSubmit == null) {
            return null;
        }
        QuestionSubmitVO questionSubmitVO = new QuestionSubmitVO();
        BeanUtils.copyProperties(questionSubmit, questionSubmitVO);
        List<String> tagList = JSONUtil.toList(questionSubmit.getTags(), String.class);
        JudgeInfo judgeInfo1 = JSONUtil.toBean(questionSubmit.getJudgeInfo(), JudgeInfo.class);
        questionSubmitVO.setTags(tagList);
        questionSubmitVO.setJudgeInfo(judgeInfo1);
        return questionSubmitVO;
    }
}
